//
//  HomeTableViewController.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/8.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import AFNetworking
import SDWebImage
import SVProgressHUD

private let cellID = "BlogCell"

class HomeTableViewController: CSYBasicTableViewController {

    //为什么要加括号
    lazy var blogDataArray = [CSYMyMicroBlogModel]()
    
    //想要上拉加载更多数据需要使用自定义的小菊花View --> 然后使用since_id刷新数据
    lazy var upDragActivityView : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    var array = [1,2,3,45]
    
    override func loadView() {
        
        super.loadView()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !GetUserInfoModel().isLogin{
        
        visitorView?.setViewItems(nil, tipsInfo: "关注一些人，回这里看看有什么惊喜")
        
            return
            
        }
        
        refreshControl = UIRefreshControl()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
        tableView.estimatedRowHeight = 200
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        let footerView : UIView = UIView(frame: CGRectMake(0, 0, screen_Width, 44))
        
        footerView.backgroundColor = UIColor.lightGrayColor()
        
        footerView.addSubview(upDragActivityView)
        
        upDragActivityView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(footerView.snp_centerX)
            
            make.centerY.equalTo(footerView.snp_centerY)
            
        }
        
        tableView.tableFooterView = footerView
        
        tableView.tableFooterView?.backgroundColor = UIColor.whiteColor()
        
        registCell()
        
        loadData()
        
    }
    
    @objc private func loadData(){
     
        //定义一个参数来获取最新的微博
        var since_id : Int64 = 0
        
        //定义一个参数来获取旧的微博
        var max_id : Int64 = 0
        
        if upDragActivityView.isAnimating(){
        
            since_id = 0
            
            max_id = blogDataArray.last?.id ?? 0
            
        }else{
            
            max_id = 0
        
            since_id = blogDataArray.first?.id ?? 0
            
        }
        
        //在传入的方法中来添加这两个参数
        GetMainPageMessageModel.loadUserData(since_id: since_id, max_id: max_id) { (array) -> () in
            
            SVProgressHUD.dismiss()
            
            self.refreshControl?.endRefreshing()
            
            if array == nil{
            
                UIView.animateWithDuration(1.25, animations: { () -> Void in
                    
                    print("________没有数据了")
                    
                    SVProgressHUD.showErrorWithStatus("哎呀..没有新的数据呢..", maskType: SVProgressHUDMaskType.Black)

                    }, completion: { (_) -> Void in
                        
                        SVProgressHUD.dismiss()
                        
                        self.upDragActivityView.stopAnimating()
                    
                })
                
                return
                
            }
            
            if since_id > 0 {
            
                self.blogDataArray = array! + self.blogDataArray
            
            }else if max_id > 0 {
            
                self.blogDataArray += array!
            
                self.upDragActivityView.stopAnimating()
                
                print(self.blogDataArray.count)
                
            }else{
            
                self.blogDataArray = array!
            
            }
            
            self.tableView.reloadData()
            
        }
        
    }

    private func registCell() {
    
        self.tableView.registerClass(CSYHomeTableViewCell.self, forCellReuseIdentifier: cellID)
        
        tableView.separatorStyle = .None
    
    }
    
    // MARK: - Table view delegate
//    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        
//        return 44
//        
//    }
    
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.blogDataArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! CSYHomeTableViewCell

        let model = blogDataArray[indexPath.row]
        
        cell.blogModel = model
        
        if indexPath.row == blogDataArray.count - 1{
        
            upDragActivityView.startAnimating()
        
            loadData()
            
            SVProgressHUD.showWithStatus("加载更多数据", maskType: SVProgressHUDMaskType.Black)
            
        }

        return cell
    }

   
    //加载网络数据
    
        
    

}
